package com.zhoufeng.udp.cient;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.zhoufeng.common.Common;
import com.zjhbkj.xinfen.util.CommandUtil;

public class ClientThread implements Runnable {
	private int count;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	@Override
	public void run() {
		try {
			DatagramSocket client;
			client = new DatagramSocket();

			// SendComsModel localRcvComsModel = new SendComsModel();
			// String s =
			// "40 DA 24 00 01 05 54 00 51 00 A0 02 00 16 0D 02 24 FF FF FF B7 AB";
			// String s =
			// "40 9A 11 24 22 33 54 22 00 00 00 00 00 00 00 00 00 FF FF FF 4E AB";
			// String s =
			// "40 DA 43 0D 0C 02 73 00 63 00 A0 02 5C 15 E9 82 25 7B 00 00 2C AB";
			// String s =
			// "40 DA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF FF BA 4B AB";

			// String s =
			// "FF FF FE 13 01 11 26 00 35 00 A0 02 5C 15 E9 82 25 FF FF BA 2C AB";
			// String s =
			// "AA EA 03 28 02 12 02 13 1B 04 0F 1E 08 1E 15 03 82 FF FF BA 33 AB";
			// String s =
			// "AA EA 0F 0A 03 01 15 11 19 05 0F 13 0E 1E 14 03 64 FF FF BA A5 AB";
			// System.out.println(s.length());

			// String s =
			// "AA 8A 11 24 22 33 54 22 01 01 01 00 00 00 00 00 00 FF FF FF 4E AB";

			// String s =
			// "AA EA 14 14 01 02 36 0C 0D 06 0F 13 0E 1E 14 00 64 03 00 00 49 AB";

//			String s = "40 DA 14 00 0B 02 0D 00 0F 00 8F 02 21 21 20 20 1D 03 00 00 70 AB";
			
			String s = "AA EA 03 2B 02 02 0F 0A 0F 06 0F 13 0E 1E 14 00 64 7B 00 00 A1 AB";
			
			
			byte[] sendBuf;
			// sendBuf = s.getBytes();
			sendBuf = CommandUtil.getCommand(s);

			// System.out.println(sendBuf.length);
			 InetAddress addr = InetAddress.getByName("127.0.0.1");
			// InetAddress addr = InetAddress.getByName("122.112.65.143");
//			InetAddress addr = InetAddress.getByName("115.159.71.155");

			int port = 5000;
			DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, addr, port);
			System.out.println("发送：次数：" + count + "，时间：" + sdf.format(new Date()) + "，数据：" + s);
			client.send(sendPacket);

			byte[] recvBuf = new byte[Common.COMMAND_LENGTH];
			DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
			client.setSoTimeout(5000);
			client.receive(recvPacket);
			// String recvStr = new String(recvPacket.getData(), 0,
			// recvPacket.getLength());
			String recvStr = CommandUtil.bytesToHexString(recvPacket.getData()); // new
																					// String(recvPacket.getData(),
																					// 0,
			System.out.println("接受：次数：" + count + "，时间：" + sdf.format(new Date()) + "，数据：" + recvStr); 
			// recvPacket.getLength());
			// System.out.println("次数：" + count + "收到:" + recvStr);
			client.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
